#!/bin/bash

echo "Start apply patch from X9E BuildSystem ..."

# 设置目录变量
PWD_DIR=$(pwd)
DIR="$PWD_DIR/../NEM11_BAU_X9E_Build_System"

if [ -d "$DIR" ]; then

    echo "Remove Old Repo : $DIR"
    rm -rf $DIR

fi

cd .. && git clone https://git-devops.zeekrlife.com/nem11-developers-group/nem11_bau_x9e_build_system.git NEM11_BAU_X9E_Build_System

if [ -d "$DIR" ]; then

    cd $DIR && git checkout X9_PTG4.3 && git branch -a

    make inittools

    make sync_all

    cd $DIR

    git apply $PWD_DIR/buildsystem_patch/buildsystem.patch

    cd $DIR/atf/atf

    git apply $PWD_DIR/buildsystem_patch/atf.patch

    cd $DIR/BareMetal_Suite

    git apply $PWD_DIR/buildsystem_patch/BareMetal_Suite.patch

    cd $DIR/res/partition

    git apply $PWD_DIR/buildsystem_patch/partition.patch

    cd $DIR/rtos/freertos_safetyos

    git apply $PWD_DIR/buildsystem_patch/freertos_safetyos.patch

    cd $DIR/rtos/lk

    git apply $PWD_DIR/buildsystem_patch/lk.patch

    cd $DIR/rtos/lk_boot

    git apply $PWD_DIR/buildsystem_patch/lk_boot.patch

    cd $DIR/rtos/lk_boot/chipcfg

    git apply $PWD_DIR/buildsystem_patch/chipcfg.patch

    cd $DIR/rtos/lk_boot/chipdev

    git apply $PWD_DIR/buildsystem_patch/chipdev.patch

    cd $DIR/rtos/lk_boot/exdev

    git apply $PWD_DIR/buildsystem_patch/exdev.patch

    cd $DIR/rtos/lk_boot/framework

    git apply $PWD_DIR/buildsystem_patch/framework.patch

    cd $DIR/rtos/lk_boot/hal

    git apply $PWD_DIR/buildsystem_patch/hal.patch

    cd $DIR/rtos/lk_boot/platform

    git apply $PWD_DIR/buildsystem_patch/platform.patch

    cd $DIR/rtos/lk_boot/target

    git apply $PWD_DIR/buildsystem_patch/target.patch

    cd $DIR/yocto/source/linux

    git apply $PWD_DIR/buildsystem_patch/linux.patch

    cd $DIR/yocto/poky

    git apply $PWD_DIR/buildsystem_patch/poky.patch

    cd $DIR/yocto/meta-semidrive

    git apply $PWD_DIR/buildsystem_patch/meta-semidrive.patch

    make build_all

fi

echo "Start apply patch from X9E BuildSystem [END]..."
